﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Kooboo.HtmlFunc.Function;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.HtmlFunc.Analysis.Visitors
{
    public class LabelVisitor : StartEndVisitor
    {
        public LabelVisitor(VisitorContext context)
            : base(context, FuncType.Label)
        {
        }

        private IFuncTester _startCommentTester;
        protected override IFuncTester StartCommentTester
        {
            get
            {
                if (_startCommentTester == null)
                    _startCommentTester = new Testers.ParametricTester(this.Func.StartComment);
                return _startCommentTester;
            }
        }

        protected override object OnQualified(HtmlCommentNode startNode, HtmlCommentNode endNode)
        {
            var tester = (Testers.ParametricTester)this.StartCommentTester;

            // add
            var label = Provider.Factory.GetProvider<ILabel>();
            label.StartNode = startNode;
            label.EndNode = endNode;
            label.Key = (tester != null) ? tester.GetParameter("key") : null;
            this.Context.Result.Labels.Add(label);

            // ret context mark
            return label;
        }

        protected override FuncType ExclusiveType
        {
            get
            {
                return this.FuncType
                    | FuncType.HtmlPart
                    | FuncType.HtmlBlockPart;
            }
        }
    }
}
